package com.juphoon.mockdata.util;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class E06Utils {

    public static String[] IntTypeArray = {"LINES", "REPEAT_NUM","DAYS"};
    public static Map<String, String> ArrayTypeArray = new HashMap<>();

    public final static Pattern PATTERN_CHAR = Pattern.compile("<(\\w+)/>");

    static {
        ArrayTypeArray.put("<AUTH_USER_ID_ARRAY>", "<AUTH_USER_ID_ARRAY attr=\"array\">");
        ArrayTypeArray.put("<LIST_ARRAY>", "<LIST_ARRAY attr=\"array\">");
        ArrayTypeArray.put("<DETAIL_ARRAY>", "<DETAIL_ARRAY attr=\"array\">");
    }

    public static String wrapXmlString(String originString) {
        //先将类似<BODY/> 改成 <BODY>JuphoonTemp</BODY>
        Matcher matcher1 = PATTERN_CHAR.matcher(originString);
        StringBuffer sb1 = new StringBuffer();
        while (matcher1.find()) {
            matcher1.appendReplacement(sb1, "<" + matcher1.group(1) + ">JuphoonTemp</" + matcher1.group(1) + ">");
        }
        matcher1.appendTail(sb1);
        originString = sb1.toString();

        //加入attr 数组型
        Iterator<Map.Entry<String, String>> iterator = ArrayTypeArray.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> next = iterator.next();
            originString = originString.replaceAll(next.getKey(), next.getValue());
        }
        //加入attr 字符串s和整数型i
        String regex = "<(?!\\/)(?!service|SYS_HEAD|LOCAL_HEAD|APP_HEAD|BODY)(\\w+)>([^<]*)<\\/\\1>";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(originString);
        StringBuffer sb = new StringBuffer(originString.length());

        while (matcher.find()) {
            String tag = matcher.group(1);
            String content = matcher.group(2);
            String type = "s";
            if (Arrays.asList(IntTypeArray).contains(tag)) {
                type = "i";
            }
            String replacement = "<" + tag + " attr=\"" + type + "," + content.length() + ",F\">" + content + "</" + tag + ">";
            matcher.appendReplacement(sb, replacement);
        }
        matcher.appendTail(sb);
        StringBuilder resultBuilder = new StringBuilder();
        resultBuilder.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n").append(sb.toString().replace("<service>", "<service version=\"2.0\">"));
        return resultBuilder.toString().replaceAll("JuphoonTemp", "");
    }

    public static HttpResponse httpGet(String address, String requestBody) {
        return HttpRequest.post(address)
                .contentType("application/xml")
                .timeout(10 * 1000)
                .setConnectionTimeout(10 * 1000)
                .setReadTimeout(10 * 1000)
                .body(requestBody)
                .execute();
    }

    public static String encrypt(String str) {
        if (StrUtil.isEmpty(str)) {
            return str;
        }
        if (str.length() < 8) {
            return str;
        }
        int start = str.length() / 3;
        int end = Math.min(str.length() / 3 * 2, 4);
        return str.substring(0, start) + "******" + str.substring(str.length() - end);
    }

    public static String formatTime(long ms) {
        int ss = 1000;
        int mi = ss * 60;
        int hh = mi * 60;
        int dd = hh * 24;

        long day = ms / dd;
        long hour = (ms - day * dd) / hh;
        long minute = (ms - day * dd - hour * hh) / mi;

        return day + "天";
    }

}
